package s47.dqj.action;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import s47.dqj.bean.DepartmentLogin;
import s47.dqj.bean.SystemLogin;
import s47.dqj.service.DepartmentLoginServiceInf;
import s47.dqj.service.SystemLoginServiceInf;

import com.opensymphony.xwork2.ActionContext;

@Controller("departmentLoginAction")
public class DepartmentLoginAction {
	
	@Resource(name="departmentLoginServiceImpl")
	private DepartmentLoginServiceInf departmentLoginService;
	@Resource(name="systemLoginServiceImpl")
	private SystemLoginServiceInf systemLoginService;
	private SystemLogin systemLogin;
	private DepartmentLogin departmentLogin;
	private String position;
	private String ip;

	public SystemLoginServiceInf getSystemLoginService() {
		return systemLoginService;
	}

	public void setSystemLoginService(SystemLoginServiceInf systemLoginService) {
		this.systemLoginService = systemLoginService;
	}

	public SystemLogin getSystemLogin() {
		return systemLogin;
	}

	public void setSystemLogin(SystemLogin systemLogin) {
		this.systemLogin = systemLogin;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public DepartmentLoginServiceInf getDepartmentLoginService() {
		return departmentLoginService;
	}

	public void setDepartmentLoginService(
			DepartmentLoginServiceInf departmentLoginService) {
		this.departmentLoginService = departmentLoginService;
	}

	public DepartmentLogin getDepartmentLogin() {
		return departmentLogin;
	}

	public void setDepartmentLogin(DepartmentLogin departmentLogin) {
		this.departmentLogin = departmentLogin;
	}

	/**
	 * 员工登录
	 * @return
	 */
	public String findLogin(){
		//返回工作站
		String temp=null;
		//System.out.println(departmentLogin.getDllogin());
		//System.out.println(departmentLogin.getDlpassword());
		this.departmentLogin=departmentLoginService.findLogin(this.departmentLogin);		
		if(this.departmentLogin!=null){
			switch(this.departmentLogin.getDpid().getDpposition()){
				case 0:{position="医生工作站";temp="doctorStation";}break;
				case 1:{position="医生工作站(主任)";temp="doctorStation1";}break;
				case 2:{position="护士工作站";temp="nurseStation";}break;
				case 3:{position="药房工作站";temp="medicalStation";}break;
				case 4:{position="药房工作站(主任)";temp="medicalStation1";}break;
				case 5:{position="检验科工作站";temp="cureStation";}break;
				case 6:{position="检验科工作站(主任)";temp="cureStation1";}break;
				case 7:{position="挂号工作站";temp="costStation";}break;
				case 8:{position="挂号工作站(主任)";temp="costStation1";}break;
				case 10:{position="管理员工作站";temp="costStation1";};break;
				default:{position="患者信息管理工作站";temp="patientInfoStation";}break;
			}
			//修改数据库中职员登录信息的ip及登录状态
			this.departmentLogin.setDlip(ip);
			this.departmentLogin.setDlloginState(1);
			this.departmentLoginService.update(this.departmentLogin);
			//System.out.println("============="+this.systemLogin);
			//保存一条登录信息至SystemLogin数据表中
			this.systemLogin=new SystemLogin();
			this.systemLogin.setDpid(this.departmentLogin.getDpid());
			this.systemLogin.setSlip(this.ip);
			this.systemLogin.setSldate(new Date());
			this.systemLoginService.save(this.systemLogin);
			
			//将登录职员信息保存至session中
			ActionContext.getContext().getSession().put("departmentLogin", this.departmentLogin.getDpid());
			//将position保存至session中
			ActionContext.getContext().getSession().put("position", this.position);
		}
		//System.out.println("============="+this.departmentLogin.getDpid().getDpname()+""+this.departmentLogin.getDpid().getDtid().getDtname());
		//System.out.println("============="+this.departmentLogin.getDpid().getDpposition());
		//System.out.println("temp=============="+temp);
		return temp;
	}
	
	public String logout(){
		this.departmentLogin=departmentLoginService.findObject(this.departmentLogin);
		
		return "logout";
	}
	
	public String test(){
		//System.out.println("test======action");
		return "patientInfoStation";
	}
}
